/*
 * @Descripttion   :
 * @version        :
 * @Author         :
 * @Date           : 2025-06-23 15:52
 * @LastEditTime   : 2025-06-23 15:52
 */

class SplitMoudle {
  constructor (socketUrl) {
    this.currentSocketUrl = socketUrl
    this.Close()
    this.ReiceiveDataHander = this.OnReceiveData.bind(this)
    this.openEventHander = this.openEvent.bind(this)

    this.planeEntities = []
    this.clippingPlanes = null
    this.currentTileset = null
    this.PlaneUpdateFunctionCallback = null
    this.currentTileset = null
    this.edgeStylingEnabled = false

    this.endEvent = null
    this.startSocket(this.currentSocketUrl)
  }

  startSocket (socketUrl) {
    console.log('正在连接：', socketUrl)
    this.currentSocketUrl = socketUrl
    this.ws = new WebSocket(socketUrl)
    this.ws.onopen = this.openEventHander
    this.ws.onmessage = this.ReiceiveDataHander
    this.ws.onclose = this.CloseEvent
    this.ws.onclose = this.errorEvent
  }

  Close () {
    if (this.ws != null) {
      console.log(this.currentSocketUrl, 'socket关闭连接')
      this.ws.close()
    }
    this.ws = null
  }

  OnReceiveData (e) {
    // console.log(e.data)
    this.endEvent?.(e.data)
    // this.ReiceiveDataManger.OnReceiveData(e.data)
  }

  openEvent (e) {
    console.log('socket连接成功', this.currentSocketUrl)
  }

  CloseEvent (e) {
    console.log('断开链接')
  }

  errorEvent (e) {
    // console.log('发生错误', e, currentSocketUrl)
  }

  SendMessage (data) {
    console.log('发送信息：', data)
    this.ws.send(data)
  }

  // DestroyModel() {
  //   this.ReiceiveDataManger.DestroyModel()
  // }

  addEventListener (type, callback) {
    type === 'end' && (this.endEvent = callback)
    // this.ReiceiveDataManger.addEventListener(type, callback)
  }
}

export default SplitMoudle
